(function( window, undefined ){  
var document = window.document;
var KissC = (function(){

var KissC = $ = function( selector, context){
	// The KissC object is the meaning of 'Keep It Simple Silly!Css!'
	return KissC.Dom.fn.init(selector,context);
};

//KissC的继承方法继承,浅拷贝
KissC.extend2=function(c, p){
	for (var prop in p){
		c[prop] = p[prop];
	}
	return c;
};
//KissC.extend3是YUI的继承方式
KissC.extend3=function(Child, Parent) {
	var F = function(){};
	F.prototype = Parent.prototype;
	Child.prototype = new F();
	Child.prototype.constructor = Child;
}
//KissC的继承方法继承，也是目前jQuery的继承方式,深拷贝,
KissC.extend=function(c,p){
	for (var i in p) { 
		if (typeof p[i] === 'object') { 
			c[i] = (p[i].constructor === Array) ? [] : {};
			KissC.extend(c[i],p[i]);
		} else {
			c[i] = p[i];
		}
	}
	return c;	
}
//KissC的成员属性
KissC.extend(KissC,{
	//KissC库的版本日期
	version:'1.01.2011',
	//空函数
	//emptyFn:function(){},
	each:function(callback){
		for(var i=0,j=this.length;i<j;i++){
			KissC.isArray(this[i])?
			this[i].each(callback):
			callback.call(this,this[i],i);
		}
	},
	//是否为数字
	isNumber:function(v){
		var reg = /^([0-9])$/;
		return reg.test(v);
	},
	//是否布尔值
	isBool:function(v){
		return (v===true || v===false);
	},
	//是否为函数
	isFunction:function(v){
		return (typeof(v)=='function');
	},
	//是否为数组
	isArray : function(v){
		//return toString.apply(v) === '[object Array]';
		return v && typeof v==='object' && typeof v.length==='number' && typeof v.splice==='function';
	},
	//是否对象
	isObject:function(v){
		return (typeof(v)=='object');
	},
	//是否为空
	isNull:function(v){
		if(v==null || v==""){
			return true;
		}
		return false;
	},
	isEmpty : function(v, allowBlank){
		return v === null || v === undefined || ((KissC.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
	},
	//是否为html节点
	/*
		1.ELEMENT_NODE(元素节点)
		2.ATTRIBUTE_NODE(属性节点)
		3.TEXT_NODE(文本节点)
		4.CDATA_SECTION_NODE
		5.ENTITY_REFERENCE_NODE
		6.ENTITY_NODE
		7.PROCESSING_INSTRUCTION_NODE
		8.COMMENT_NODE(注释节点)
		9.DOCUMENT_NODE(document节点)
		10.COCUMENT_TYPE_NODE(document type 节点)
		11.DOCUMENT_FRAGMENT_NODE
		12.NOTATION_NODE
	*/
	isDom:function(node){
		return node.nodeType?true:false;
	},
	mouseEvent:function(e){
		var ev={};
		if(e.pageX){
			ev.x=e.pageX;
			ev.y=e.pageY;
			ev.target=e.target;
		}else{
			ev.x=e.clientX + document.body.scrollLeft - document.body.clientLeft;
			ev.y=e.clientY + document.body.scrollTop  - document.body.clientTop;
			ev.target=e.srcEelment;
		}
		return ev;	
	}
});
//DOM相关操作
KissC.Dom=function(selector,context){
	return KissC.Dom.fn.init(selector,context);
};
KissC.Dom.fn={
	init:function(selector,context){
		selector = selector || document;
		if (selector.nodeType) {
			this[0] = selector;
			this.el=this[0];
			this.length = 1;
			return this;//this指代KissC.Dom.fn对象
		}
		if (typeof selector == "string" ){
			return KissC.Dom(document.getElementById(selector));
		}else{
			alert('DOM error');
		}
	},
	getElementsByClass:function(theClass){
		var elementArray=[];
		var matchedArray=[];
		var pattern = new RegExp("(^| )" + theClass + "( |$)");
		if(typeof document.all !="undefined"){
			elementArray = document.all;//ie不支持*,但提供特殊的document.all
		}
		else {
			elementArray = document.getElementsByTagName("*");//提供非ie浏览器选择所有元素
		}
		for(var i=0;i<elementArray.length;i++){
			if(pattern.test(elementArray[i].className)){
				matchedArray[matchedArray.length]=elementArray[i];	
			}
		}
		return matchedArray;
	},
	//公开属性
	isReady: false,//判断onDOMReady方法是否已被执行过
	readyList:[],//把要执行的方法暂放在这个数组里
	timer:null,//定时器
	//公开方法
	ready:function(handler){
		if(document.addEventListener){
			document.addEventListener("DOMContentLoaded",function(){
				document.removeEventListener( "DOMContentLoaded", arguments.callee, false );//清除加载函数
				bindReady();
			},false);
		}else if(document.attachEvent){
			document.attachEvent("onreadystatechange",function(){
				if((/loaded|compete/).test(document.readyState)) bindReady();	   
			});	
			if(window==window.top){
				timer=setInterval(function(){
					try{
						isReady||document.documentElement.doScroll('left');
					}catch(e){return;}
					bindReady();
				},5);	
			}
		}
		if(KissC.Dom.fn.isReady) handler.call(document);
		else KissC.Dom.fn.readyList.push(function(){ return handler.call(this);});
	
		var onDOMReady=function(){
			for(var i=0;i<KissC.Dom.fn.readyList.length;i++){
				KissC.Dom.fn.readyList[i].apply(document);	
			}
			KissC.Dom.fn.readyList=null;
		}
		var bindReady=function(){
			if(KissC.Dom.fn.isReady) return;
			KissC.Dom.fn.isReady=true;
			onDOMReady.call(window);
			if(document.removeEventListener){
				document.removeEventListener("DOMContentLoaded",bindReady,false);	
			}else if(document.attachEvent){
				document.detachEvent("onreadystatechange",bindReady);
				if(window == window.top){
					clearInterval(KissC.Dom.fn.timer);
					KissC.Dom.fn.timer=null;
				}
			}
		};
	},
	hasClass:function(theClass){
		var pattern = new RegExp("(^| )" + theClass + "( |$)");	
		if(pattern.test(this.el.className)){
			return true;
		}else{
			return false;	
		}
	},
	addClass:function(theClass){
		if(!KissC(this.el).hasClass(theClass)){
			if(this.el.className==""){
				this.el.className=theClass;	
			}else{
				this.el.className+=" "+theClass;		
			}
			return KissC(this.el);
		}
	},
	removeClass:function(theClass){
		var pattern = new RegExp("(^| )" + theClass + "( |$)");
		this.el.className = this.el.className.replace(pattern, "$1");
		this.el.className = this.el.className.replace(/ $/, "");	
		return KissC(this.el);
	},
	//设置或获取el的html
	html:function(iHtml){
		var tpf=typeof(iHtml);
		if(tpf=='string' || tpf=='number' || tpf=='boolean'){
			this.el.innerHTML=iHtml;
			return KissC(this.el);
			/*var oldElem=this.el;
			if(!+"\v1"){
				oldElem.innerHTML = iHtml;
			}else{
				var newElem=oldElem.cloneNode(true);
				newElem.innerHTML = iHtml;
				oldElem.parentNode.replaceChild(newElem,oldElem);				
				return KissC(newElem);
			}*/
		}else{
			return this.el.innerHTML;
		}
	},
	//设置或获取dom对象的style属性
	css:function(prop){
		if(typeof(prop)==='string'){
			if(!this.el.currentStyle){
				return eval("document.defaultView.getComputedStyle(this.el,null)."+prop);
			}else{
				return eval("this.el.currentStyle."+prop);
			}
		}else if(typeof(prop)==='object'){
			var cssT='';
			for(var name in prop){
				if(name == "opacity" && !+"\v1"){//IE7 bug:filter 滤镜要求 hasLayout=true 方可执行（否则没有效果）
					if (!this.el.currentStyle || !this.el.currentStyle.hasLayout) this.el.style.zoom = 1;
					name = "filter";
					if(!!window.XDomainRequest){
						prop[name] ="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity="+prop[name]*100+")";
					}else{
						prop[name] ="alpha(opacity="+prop[name]*100+")";
					}
				}
				cssT +=name + ":" + prop[name]+';';	
			}
			this.el.style.cssText +=cssT;
			return KissC(this.el);
		}
	},
	/*名称:为DOM注册事件.
		参数:
			elm			dom元素
			evType		    事件名
			fn	句柄函数
			useCapture	是否使用捕获
		返回:无
	*/
	addEvent:function(elm, evType, fn, useCapture) {
		if(elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);//DOM2.0
			return true;
		}else if(elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);//IE5+
			return r;
		}else{
			elm['on' + evType] = fn;//DOM 0
		}
	},click:function(fn){
		KissC(this.el).addEvent(this.el,'click',fn);
		return KissC(this.el);
	},
	mouseover:function(fn){
		KissC(this.el).addEvent(this.el,'mouseover',fn);
		return KissC(this.el);
	},
	mouseout:function(fn){
		KissC(this.el).addEvent(this.el,'mouseout',fn);
		return KissC(this.el);
	},
	mousemove:function(fn){
		KissC(this.el).addEvent(this.el,'mousemove',fn);
		return KissC(this.el);
	},
	mouseup:function(fn){
		KissC(this.el).addEvent(this.el,'mouseup',fn);
		return KissC(this.el);
	},
	//Animation
	//暂存元素原始高度,初始值为0
	_height:0,
	fadeIn:function(speed, opacity){
		var elem=KissC(this.el).el;
		speed=(speed=='slow')?30:(speed=='fast'?10:(speed=='normal')?20:(speed||20));
		opacity=opacity || 0;
		var val=KissC(elem).css('opacity')*200;
		if(opacity<val/200){
			(function(){
				elem.style.opacity=val/200;
				val -= 5;
				if(val >= opacity*200){
					setTimeout(arguments.callee,speed);
				}
			})();
		}
	},
	fadeOut:function(speed, opacity){
		var elem=KissC(this.el).el;
		speed=(speed=='slow')?30:(speed=='fast'?10:(speed=='normal')?20:(speed||20));
		opacity=opacity || 1;
		var val=KissC(elem).css('opacity')*200;
		if(opacity>val/200){
			(function(){
				elem.style.opacity=val/200;
				val += 5;
				if(val <= opacity*200){
					setTimeout(arguments.callee,speed);
				}
			})();
		}
	},
	slideUp:function(speed, toheight){
		var elem=KissC(this.el).el;
		speed=(speed=='slow')?15:(speed=='fast'?1:(speed=='normal')?10:(speed||15));
		toheight=toheight || 0;
		var val=parseInt(KissC(elem).css('height'));
		var cut=(val-toheight)/40;
		this._height=val;
		elem.style.overflow='hidden';
		if(toheight<val){
			(function(){
				val -= cut;
				elem.style.height=val +'px';
				if(val >= toheight){
					setTimeout(arguments.callee,speed);
				}else if(parseInt(KissC(elem).css('height'))!=toheight){
					KissC(elem).css({'height':toheight+'px'})
				}
			})();
		}
	},
	slideDown:function(speed, toheight){
		var elem=KissC(this.el).el;
		speed=(speed=='slow')?30:(speed=='fast'?10:(speed=='normal')?20:(speed||20));
		toheight=toheight || this._height;
		var val=parseInt(KissC(elem).css('height'));
		var cut=(toheight-val)/40;
		elem.style.overflow='hidden';
		if(toheight>val){
			(function(){
				val += cut;
				elem.style.height=val +'px';
				if(val <= toheight){
					setTimeout(arguments.callee,speed);
				}else if(parseInt(KissC(elem).css('height'))!=toheight){
					KissC(elem).css({'height':toheight+'px'})
				}
			})();
		}
	}


};
KissC.Dom.ready=KissC.Dom.fn.ready;

	

return (window.KissC =window.kissc = window.$$ = KissC);
})();


})(window);